function t(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

function e(t, e) {
    return e.map(function(e) {
        return {
            text: e,
            lists: e.split(t).reduce(function(e, r) {
                return e.concat([ {
                    type: 1,
                    text: r
                }, {
                    type: 0,
                    text: t
                } ]);
            }, []).slice(0, -1)
        };
    });
}

var r = function() {
    function t(t, e) {
        var r = [], a = !0, i = !1, n = void 0;
        try {
            for (var s, o = t[Symbol.iterator](); !(a = (s = o.next()).done) && (r.push(s.value), 
            !e || r.length !== e); a = !0) ;
        } catch (t) {
            i = !0, n = t;
        } finally {
            try {
                !a && o.return && o.return();
            } finally {
                if (i) throw n;
            }
        }
        return r;
    }
    return function(e, r) {
        if (Array.isArray(e)) return e;
        if (Symbol.iterator in Object(e)) return t(e, r);
        throw new TypeError("Invalid attempt to destructure non-iterable instance");
    };
}(), a = t(require("../../utils/api")), i = t(require("../../utils/api-code")), n = require("./cache"), s = [ "qq.com", "gmail.com", "126.com", "163.com", "hotmail.com" ], o = {
    corpname: {
        api: a.default.receipt.searchsuggest
    },
    position: {
        api: a.default.weappcard.searchposition
    },
    email: {
        api: function(t) {
            var e = t.keyword.split("@"), a = r(e, 2), n = a[0], o = a[1], u = void 0 === o ? "" : o, c = s.filter(function(t) {
                return t.includes(u);
            }).map(function(t) {
                return n + "@" + t;
            });
            return Promise.resolve({
                code: i.default.SUCCESS,
                data: {
                    lists: c
                }
            });
        }
    }
};

Component({
    properties: {
        value: {
            type: String,
            value: "",
            observer: "_valueChange"
        },
        maxNum: {
            type: Number,
            value: 4
        },
        mask: {
            type: Boolean,
            value: !0
        },
        markstyle: String,
        name: String,
        direction: {
            type: String,
            value: "bottom"
        }
    },
    data: {
        searchList: [],
        first: !0
    },
    ready: function() {
        n.cache[this.data.name] = this;
    },
    methods: {
        onSelect: function(t) {
            var e = t.currentTarget.dataset.index;
            this.data.first = !0;
            var r = this.data, a = r.name, i = r.searchList;
            this.triggerEvent("select", {
                name: a,
                value: i[e].text
            }, {}), this.onHide();
        },
        onHide: function() {
            this.setData({
                searchList: []
            });
        },
        clearOtherList: function() {
            var t = this.data.name;
            Object.keys(n.cache).filter(function(e) {
                return e !== t;
            }).filter(function(t) {
                return n.cache[t];
            }).forEach(function(t) {
                var e = n.cache[t];
                e && e.data && e.data.searchList.length && e.setData({
                    searchList: []
                });
            });
        },
        _valueChange: function(t, e) {
            var r = this.data, a = r.first, i = r.name;
            return a && t ? this.data.first = !1 : (this.data.first = !1, t ? (this.clearOtherList(), 
            void this.request(t, e, i)) : this.onHide());
        },
        request: function(t, r, a) {
            var n = this, s = o[a];
            t !== r && s && (clearTimeout(this.timer), this.triggerEvent("show", {
                name: a
            }), this.timer = setTimeout(function() {
                s.api({
                    keyword: t
                }).then(function(r) {
                    r.code === i.default.SUCCESS && t === n.data.value && n.setData({
                        searchList: e(t, r.data.lists)
                    });
                });
            }, 500));
        }
    }
});